import logging
import math
from typing import Optional, List

from fastapi import APIRouter, Depends, Query, Path
from pydantic import Field, BaseModel
from sqlalchemy import text
from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy.orm import Session
from commons.BaseResponse import BaseResponse
from commons.PaginationBase import PaginationBase
from datebase import get_db
from utils.EncryptionUtil import oauth2_scheme, EncryptionMiddleware

charge_detail_router = APIRouter(prefix="/chargeDetail", tags=["收费单管理"], dependencies=[Depends(oauth2_scheme)])


class TriggerSceneSearch(PaginationBase):
    # 筛选条件
    fbillno: Optional[str] = Field("", description="单号")


@charge_detail_router.get("/list", summary="收费单查询接口")
def list_charge_detail(search: TriggerSceneSearch = Query(), db: Session = Depends(get_db)):
    param = {}
    fbillno = search.fbillno
    sql = "select fbillno,fline,fchargeitem,fchargeinfo,fchargeqty,frealbegtime,"
    sql = sql + " frealendtime,fplanamt,frealamt,fmemo,foperator,fupddate"
    sql = sql + " from m_tc_charge_detail"
    sql = sql + " where 1=1"
    sql = sql + " and fbillno= concat('%', :fbillno ,'%')"
    param["fbillno"] = fbillno
    data = db.execute(text(sql), param).fetchall()

    data_list = []
    for key in range(len(data)):
        d1 = {}
        d1['fbillno'] = data[key][0]
        d1['fline'] = data[key][1]
        d1['fchargeitem'] = data[key][2]
        d1['fchargeinfo'] = data[key][3]
        d1['fchargeqty'] = data[key][4]
        d1['frealbegtime'] = data[key][5]
        d1['frealendtime'] = data[key][6]
        d1['fplanamt'] = data[key][7]
        d1['frealamt'] = data[key][8]
        d1['fmemo'] = data[key][9]
        d1['foperator'] = data[key][10]
        d1['fupddate'] = data[key][11]
        data_list.append(d1)

    info_dict = {"list": data_list}
    return BaseResponse.success(data=info_dict)
